package com.mnsq.common.tools.valid;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zj on 2018/5/24 0024.
 */
public class ValidDataParam {

    private ValidDataParam() {
    }

    /**
     * 正则表达式：验证手机号
     */
    private static final String MOBILE_CHECK = "^((1[3-9][0-9]))\\d{8}$";

    /**
     * 正则表达式：验证邮箱
     */
    private static final String EMAIL_CHECK = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";

    /**
     * 正则表达式：验证身份证
     */
    public static final String ID_CARD_CHECK = "(^\\d{18}$)|(^\\d{15}$)";

    public static boolean wtIdCard(String idCard) {
        return wtCheckPass(ID_CARD_CHECK, idCard);
    }

    public static boolean wtMobilePhone(String mobilePhone) {
        return wtCheckPass(MOBILE_CHECK, mobilePhone);
    }

    public static boolean wtEmail(String email) {
        return wtCheckPass(EMAIL_CHECK, email);
    }

    private static boolean wtCheckPass(String reg, String obj) {
        if (obj == null) {
            return false;
        }
        boolean flag;
        try {
            Pattern regex = Pattern.compile(reg);
            Matcher matcher = regex.matcher(obj);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 判断输入数据类型
     *
     * @param str
     * @return 1、身份证；2、手机号；3、邮箱
     */
    public static int checkResult(String str) {
        if (wtCheckPass(ID_CARD_CHECK, str)) {
            return 1;
        } else if (wtCheckPass(MOBILE_CHECK, str)) {
            return 2;
        } else if (wtCheckPass(EMAIL_CHECK, str)) {
            return 3;
        }
        return 0;
    }

    /**
     * 获取非空或者默认对象
     *
     * @param a
     * @param defaultVal
     * @return
     */
    public static Object getNonNull(Object a, Object defaultVal) {
        if (a == null) {
            return defaultVal;
        }
        return a;
    }
}
